﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  CompositeMessageSenderTest.cs
//    
// Abstract:
//
//  This class contains unit tests for CompositeMessageSender.
//  
//--

using System.Collections.Generic;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders.Dispatcher.UnitTests
{       
    /// <summary>
    ///This is a test class for CompositeMessageSenderTest and is intended
    ///to contain all CompositeMessageSenderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CompositeMessageSenderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        /// Test a CompositeMessage sender that contains only one channel and
        /// successfully delivers the notification.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithOneChannelSuccess()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Communicator);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new MockMessageSender(expectedResult));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        /// <summary>
        /// Test a CompositeMessage sender that contains only one channel and
        /// fails to delivers the notification.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithOneChannelFailure()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new MockMessageSender(expectedResult));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        /// <summary>
        /// Test a CompositeMessage sender that contains two channels and
        /// successfully delivers the notification on the first channel.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithTwoChannelsFirstSucceeds()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Communicator);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new MockMessageSender(expectedResult));
            channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Phone)));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        /// <summary>
        /// Test a CompositeMessage sender that contains two channels and
        /// successfully delivers the notification on the second channel.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithTwoChannelsSecondSucceeds()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Phone);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator)));
            channels.Add(new MockMessageSender(expectedResult));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        /// <summary>
        /// Test a CompositeMessage sender that contains two channels and
        /// fails to deliver a notification on either channel.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithTwoChannelsNoneSucceed()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Phone);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator)));
            channels.Add(new MockMessageSender(expectedResult));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        /// <summary>
        /// Test a CompositeMessage sender that contains three channels and
        /// successfully delivers the notification on the second channel.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithThreeChannelsMiddleSucceeds()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Phone);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator)));
            channels.Add(new MockMessageSender(expectedResult));
            channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Phone)));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        /// <summary>
        /// Test a CompositeMessage sender that contains two channels, where the first succeeds, but is not a stop
        /// point, meaning that the second should execute.
        /// </summary>
        [TestMethod()]
        public void TestCompositeWithFalseStopPointSuccess()
        {
            NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Phone);

            IList<IMessageSender> channels = new List<IMessageSender>();
            channels.Add(new NotStopPointMockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Communicator)));
            channels.Add(new MockMessageSender(expectedResult));

            ExecuteCompositeMessageSenderTest(expectedResult, channels);
        }

        private static void ExecuteCompositeMessageSenderTest(NotificationResult expectedResult, IList<IMessageSender> channels)
        {
            using (AutoResetEvent waitEvent = new AutoResetEvent(false))
            {
                CompositeMessageSender messageSender = new CompositeMessageSender(channels);
                NotificationResult actualResult = null;

                messageSender.DeliverMessageCompleted += delegate(object sender, DeliveryCompletedEventArgs e)
                {
                    actualResult = e.Result;
                    waitEvent.Set();
                };
                messageSender.DeliverMessageAsync();

                waitEvent.WaitOne(1000, false);

                Assert.IsNotNull(actualResult);
                Assert.AreEqual(expectedResult.HowContacted, actualResult.HowContacted);
                Assert.AreEqual(expectedResult.Status, actualResult.Status);
            }
        }
    }
}
